package com.yangtzeu.jwc.business;

import java.util.Map;

import org.apache.http.Header;

import android.content.Context;

import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.PersistentCookieStore;
import com.loopj.android.http.RequestParams;
import com.yangtzeu.jwc.domain.MyJson;
import com.yangtzeu.jwc.utils.G;
import com.zjsx.lib.utils.DataUtil;
import com.zjsx.lib.utils.L;
import com.zjsx.lib.utils.NetUtil;

public class HttpBusiness {
    private AsyncHttpClient client;
    private PersistentCookieStore cookieStore;
    private Context context;

    public HttpBusiness(Context context) {
        this.context = context;
        this.cookieStore = new PersistentCookieStore(this.context);
        this.client = getNewClient();
    }

    public AsyncHttpClient getNewClient() {
        AsyncHttpClient client = new AsyncHttpClient();
        client.setTimeout(20 * 1000);
        client.setMaxRetriesAndTimeout(0, 20 * 1000);
        client.setCookieStore(cookieStore);
        return client;
    }

    /**
     * 无参数的Http参数
     *
     * @param url
     * @param callBack
     */
    public void doHttpRequest(String url, final MCallBack callBack) {
        doHttpRequest(url, null, callBack);
    }

    /**
     * 执行Http请求 ， 默认都是使用的post的方式
     *
     * @param url
     * @param paramsMap
     * @param callBack
     */
    public void doHttpRequest(final String url, final Map<String, String> paramsMap, final MCallBack callBack) {
        L.d("request url：" + url);
        RequestParams params = new RequestParams(paramsMap);
        client.post(url, params, new AsyncHttpResponseHandler() {

            @Override
            public void onFailure(int arg0, Header[] arg1, byte[] arg2, Throwable arg3) {
                if (com.zjsx.lib.utils.G.DEBUG) {
                    String failed = "url:" + url;
                    if (paramsMap != null) {
                        failed += "\n" + paramsMap.toString();
                    }
                    if (arg2 != null) {
                        failed += "\ndata:" + new String(arg2);
                    }
                    L.d(failed);
                    if (arg3 != null) {
                        arg3.printStackTrace();
                    }
                }
                if (!NetUtil.isNetConnected(context)) {
                    callBack.onFailure(arg2, "您的网络未连接，请先设置网络", false);
                } else {
                    callBack.onFailure(arg2, "请求错误，请检查教务处是否能够访问", false);
                }
            }

            @Override
            public void onProgress(int bytesWritten, int totalSize) {
                callBack.onProgress(bytesWritten, totalSize);
            }

            @Override
            public void onSuccess(int arg0, Header[] arg1, byte[] arg2) {
                try {
                    MyJson mj = DataUtil.dataToObj(arg2, MyJson.class);
                    if (com.zjsx.lib.utils.G.DEBUG) {
                        L.d("Success：" + url + "\nparams:" + paramsMap + "\ndata:" + mj.toString());
                    }
                    callBack.onSuccess(arg2, "请求成功", false, mj);
                } catch (Exception e) {
                    callBack.onFailure(arg2, "数据错误", false);
                    L.d("error data:" + new String(arg2));
                    e.printStackTrace();
                }
            }

        });
    }
}
